package com.ctl.szmd.szmd.feature_wm.data.repository

import androidx.paging.ExperimentalPagingApi
import androidx.paging.LoadType
import androidx.paging.PagingState
import androidx.paging.RemoteMediator

/**
 * created by : chentl
 * Date: 2020/08/05
 */

@OptIn(ExperimentalPagingApi::class)
class RemoteMediator<T : Any> : RemoteMediator<Int, T>() {
    override suspend fun load(loadType: LoadType, state: PagingState<Int, T>): MediatorResult {

        when (loadType) {
            // 首次访问 或者调用 PagingDataAdapter.refresh()
            LoadType.REFRESH -> null

            // 在当前加载的数据集的开头加载数据时
            LoadType.PREPEND -> return MediatorResult.Success(endOfPaginationReached = true)

            LoadType.APPEND -> { // 下来加载更多时触发
                val lastItem = state.lastItemOrNull()
                    ?: return MediatorResult.Success(
                        endOfPaginationReached = true
                    )
            }
        }
        return MediatorResult.Success(endOfPaginationReached = false)
    }
}